package cn.topcode.unicorn.utils;

import org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CollectionUtil {

	/**
	 * 将对象集合中每个对象的指定属性取出作为列表
	 * @param list	对象集合
	 * @param propName	对象属性
	 * @return	对象属性值列表
	 */
	public static <T> List<T> getPropList(Collection<?> list, String propName) {
		List<T> lstResult = new ArrayList<T>();
		for (Object obj : list) {
			try {
				T val = (T) PropertyUtils.getSimpleProperty(obj, propName);
				lstResult.add(val);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return lstResult;
	}
	
	/**
	 * 将字符串数组，转换成用逗号隔开的字符串
	 * 
	 * @param strArray
	 * @return
	 */
	public static String array2String(String[] strArray) {
		StringBuilder sbTmp = new StringBuilder();
		if (strArray != null) {
			for (String str : strArray) {
				if (str != null) {
					if (sbTmp.length() > 0) {
						sbTmp.append(",");
					}
					sbTmp.append(str.trim());
				}
			}
		}
		return sbTmp.toString();
	}
	
	public static String array2String(Collection<String> strArray, String split) {
		StringBuilder sbTmp = new StringBuilder();
		if (strArray != null) {
			for (String str : strArray) {
				if (str != null) {
					if (sbTmp.length() > 0) {
						sbTmp.append(split);
					}
					sbTmp.append(str.trim());
				}
			}
		}
		return sbTmp.toString();
	}

	public static String array2String(List<String> strArray) {
		StringBuilder sbTmp = new StringBuilder();
		if (strArray != null) {
			for (String str : strArray) {
				if (str != null) {
					if (sbTmp.length() > 0) {
						sbTmp.append(",");
					}
					sbTmp.append(str.trim());
				}
			}
		}
		return sbTmp.toString();
	}
}
